﻿using System;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTcms.Common;

namespace DTcms.Web.admin.shiming
{
    public partial class shiming_list : Web.UI.ManagePage
    {
        protected int totalCount;
        protected int page;
        protected int pageSize;
        protected string keywords = string.Empty;
        protected int status;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.keywords = DTRequest.GetQueryString("keywords");
            this.status = DTRequest.GetQueryInt("status");

            this.pageSize = GetPageSize(10); //每页数量
            if (!Page.IsPostBack)
            {
                RptBind("id>7" + CombSqlTxt(this.status, keywords), "  add_time desc,id desc");
            }
        }

       

        #region 数据绑定=================================
        private void RptBind(string _strWhere, string _orderby)
        {
            this.page = DTRequest.GetQueryInt("page", 1);
            txtKeywords.Text = this.keywords;
            //ddlQuanziID.SelectedValue = this.quanzi_id.ToString();
            BLL.shiming bll = new BLL.shiming();
            this.rptList.DataSource = bll.GetList(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            this.rptList.DataBind();

            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("shiming_list.aspx", "keywords={0}&page={1}", this.keywords, "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt(int _status, string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and real_name like '%" + _keywords + "%'");
            }
            if (_status > 0)
            {
                strTemp.Append(" and status=" + _status);
            }
            return strTemp.ToString();
        }
        #endregion

        #region 返回每页数量=============================
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("ad_page_size", "DTcmsPage"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion

        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("shiming_list.aspx", "keywords={0}", txtKeywords.Text));
        }

        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("ad_page_size", "DTcmsPage", _pagesize.ToString(), 14400);
                }
            }
            Response.Redirect(Utils.CombUrlTxt("shiming_list.aspx", "keywords={0}", this.keywords, this.status.ToString()));
        }

        //保存排序
        protected void btnSave_Click(object sender, EventArgs e)
        {
            BLL.shiming bll = new BLL.shiming();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    BLL.users ubll = new BLL.users();
                    Model.shiming smodel = bll.GetModel(id);
                    if (smodel != null )
                    {
                        bll.UpdateField(id, "status=99");
                        ubll.UpdateField(smodel.user_id, "shiming_status=99,shiming_id=" + id);
                    }
                }
                   
            }
            JscriptMsg("审核成功！", Utils.CombUrlTxt("shiming_list.aspx", "keywords={0}", this.keywords), "parent.loadMenuTree");
        }
        //审核通过
        protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            BLL.shiming bll = new BLL.shiming();
            string _action = e.CommandName.ToString();
            int id = int.Parse(e.CommandArgument.ToString());
            if (_action == "btnok")
            {
                BLL.users ubll = new BLL.users();
                Model.shiming smodel = bll.GetModel(id);
                if (smodel != null && smodel.status < 99)
                {
                    bll.UpdateField(id, "status=99");
                    ubll.UpdateField(smodel.user_id, "shiming_status=99,shiming_id=" + id);
                    JscriptMsg("已审核通过！", Utils.CombUrlTxt("shiming_list.aspx", "keywords={0}", this.keywords), "parent.loadMenuTree");
                }

            }
            else if (_action == "btnfail") {
                BLL.users ubll = new BLL.users();
                Model.shiming smodel = bll.GetModel(id);
                if (smodel != null && smodel.status < 99)
                {
                    bll.UpdateField(id, "status=2");
                    ubll.UpdateField(smodel.user_id, "shiming_status=2,shiming_id=" + id);
                    JscriptMsg("已审核退回！", Utils.CombUrlTxt("shiming_list.aspx", "keywords={0}", this.keywords), "parent.loadMenuTree");
                }
            }
        }
        public string GetshimingstatusText(string _status) {
            string flag = "";
            if (_status =="1") { flag = "<font color='#ff0000'>待审核</font>"; }
            else if (_status == "2") { flag = "<font color='#080808'>已退回</font>"; }
            else if (_status == "99") { flag = "<font color='#7CCD7C'>已审核</font>"; }
            return flag;
        }

        protected void ddlstatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("shiming_list.aspx", "status={0}&keywords={1}", ddlstatus.SelectedValue, this.keywords));
        }

        //批量删除
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int sucCount = 0;
            int errorCount = 0;
            BLL.shiming bll = new BLL.shiming();
            string tips = "";
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    Model.shiming smodel = bll.GetModel(id);
                    if (smodel.status == 2)
                    {
                        BLL.users ubll = new BLL.users();
                        if (bll.Delete(id)) { sucCount += 1; ubll.UpdateField(smodel.user_id, "shiming_status=0,shiming_id=0"); }
                        else { errorCount += 1; }
                    }
                    else { errorCount += 1; tips = "提示：只有实名认证退回的才能进行删除！"; }
                    
                }
            }
            JscriptMsg("删除成功" + sucCount + "条，失败" + errorCount + "条！"+ tips, Utils.CombUrlTxt("shiming_list.aspx", "keywords={1}", this.keywords), "parent.loadMenuTree");
        }

        //protected void ddlQuanziID_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    Response.Redirect(Utils.CombUrlTxt("shiming_list.aspx", "quanzi_id={0}&keywords={1}", ddlQuanziID.SelectedValue, this.keywords));
        //}
    }
}